package com.daidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.daidai.dao.AssociationDao;
import com.daidai.model.Association;
import com.daidai.service.AssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 17:45
 * Description:
 */
@Service
public class AssociationServiceImpl implements AssociationService {

    @Autowired
    private AssociationDao associationDao;
    @Override
    public int findOwnerIdByAssociationId(int associationId) {
        return associationDao.selectOne(new LambdaQueryWrapper<Association>().eq(Association::getAssociationId, associationId)).getOwnerId();
    }

    @Override
    public Association findAssociationById(int associationId) {
        return associationDao.selectById(associationId);
    }

    @Override
    public Association findAssByUserId(int userId) {
        return associationDao.selectOne(new LambdaQueryWrapper<Association>().eq(Association::getOwnerId, userId));
    }

    @Override
    public String findSignatureActivityName(int associationId) {
        return associationDao.findSignatureActivityName(associationId);
    }

    @Override
    public List<Association> findAllAssociations() {
        return associationDao.selectList(new LambdaQueryWrapper<Association>().eq(Association::getAssociationStatus, 1));
    }

    @Override
    public List<Association> findAssociationsByType(int type) {
        return associationDao.selectList(new LambdaQueryWrapper<Association>().eq(Association::getType, type));
    }

    @Override
    public String findAssociationNameByAssociationId(int associationId) {
        return associationDao.selectById(associationId).getName();
    }

    @Override
    public List<Association> findAllAss() {
        return associationDao.selectList(null);
    }

    @Override
    public List<Association> findAllAssociationsApply() {
        return associationDao.selectList(new LambdaQueryWrapper<Association>().eq(Association::getAssociationStatus, 0));
    }

    @Override
    public void addAssociation(String description, String name, String createTime, int type, int ownerId, String logo) {
        Association association = new Association();
        association.setDescription(description)
                .setName(name)
//                .setCreateTime(createTime)
                .setType(type)
                .setOwnerId(ownerId)
                .setLogo(logo);
        associationDao.insert(association);
    }

    @Override
    public void addAss(String description, String name, String createTime, int type, int ownerId, String logo) {
        Association association = new Association();
        association.setDescription(description)
                .setName(name)
                .setCreateTime(createTime)
                .setType(type)
                .setOwnerId(ownerId)
                .setLogo(logo)
                .setSignatureActivity(1);
        associationDao.insert(association);
    }

    @Override
    public void deleteAss(int associationId) {
        associationDao.deleteById(associationId);
    }

    @Override
    public void applyPass(String createTime, int associationId) {
        Association association = new Association();
        association.setCreateTime(createTime)
                .setAssociationStatus(1)
                .setAssociationId(associationId);
        associationDao.updateById(association);
    }

    @Override
    public void applyRefuse(int associationId) {
        associationDao.updateById(new Association().setAssociationId(associationId).setAssociationStatus(2));
    }

    @Override
    public void assModify(String description, String name, int type, int ownerId, String logo, Integer signatureActivity, int associationId) {
        Association association = new Association();
        association.setDescription(description)
                .setName(name)
                .setType(type)
                .setOwnerId(ownerId)
                .setLogo(logo)
                .setSignatureActivity(signatureActivity)
                .setAssociationId(associationId);
        associationDao.updateById(association);
    }
}
